﻿using Grpc.Core;
using Grpc.Core.Interceptors;

namespace GrpcServer_Demo;

public class ParamInterceptor : Interceptor
{
    public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
        TRequest request,
        ServerCallContext context,
        UnaryServerMethod<TRequest, TResponse> continuation)
    {
        var requestParams = request;
        if (NotNullCheck(requestParams))
        {
            throw new RpcException(new Status(StatusCode.InvalidArgument, "Unimplemented"));
        }
       
        return await continuation(request, context);
    }

    static bool NotNullCheck(object obj)
    {
        foreach (var propertyInfo in obj.GetType().GetProperties())
        {
            var value = propertyInfo.GetValue(obj);
            if (value == null)
            {
                return true;
            }

            if (value is string s)
            {
                if (string.IsNullOrWhiteSpace(s))
                {
                    return true;
                }
            }
        }

        
        return false;
    }
}